package dialogs;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.NonNull;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.newxlmedical.OpenDoorActivity;
import com.example.administrator.newxlmedical.OpenDoorRemindActivity;
import com.example.administrator.newxlmedical.R;

import beans.NewUserInfoBean;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import constants.Constants;
import constants.HandlerConstant;
import custom.MyDialog;
import customhandler.MyHandler;
import singleton.SingletonClass;
import utils.ActivityUtil;
import utils.FormateUtils;
import utils.MediaPlayerUtils;
import utils.ObjectSaveUtil;

/**
 * Created by Administrator on 2018/10/17.
 */

public class FasterOpenDoorDialog extends MyDialog {

    @BindView(R.id.iv_faster_opendoor_1)
    ImageView ivFasterOpendoor1;
    @BindView(R.id.tv_faster_opendoor_1)
    TextView tvFasterOpendoor1;
    @BindView(R.id.iv_faster_opendoor_2)
    ImageView ivFasterOpendoor2;
    @BindView(R.id.tv_faster_opendoor_2)
    TextView tvFasterOpendoor2;
    @BindView(R.id.iv_faster_opendoor_3)
    ImageView ivFasterOpendoor3;
    @BindView(R.id.tv_faster_opendoor_3)
    TextView tvFasterOpendoor3;
    @BindView(R.id.iv_faster_opendoor_4)
    ImageView ivFasterOpendoor4;
    @BindView(R.id.tv_faster_opendoor_4)
    TextView tvFasterOpendoor4;
    @BindView(R.id.iv_faster_opendoor_5)
    ImageView ivFasterOpendoor5;
    @BindView(R.id.tv_faster_opendoor_5)
    TextView tvFasterOpendoor5;
    @BindView(R.id.iv_faster_close)
    ImageView ivFasterClose;

    //用于判断是否可点击
    private boolean isClickOne, isClickSecond, isClickThird, isClickFour, isClickFive;
    private MyHandler allHandler = SingletonClass.getSingleton(MyHandler.class);
    //用于判断选中的是哪个柜子
    private int selectCabinet = 0;
    //用于判断上一次选中的是哪个柜子
    private int beforeSelect = 0;
    private SharedPreferences sharedPreferences;
    private NewUserInfoBean.DataBean userInfoBean;
    private MediaPlayer mediaPlayer;

    public FasterOpenDoorDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_faster_opendoor);
        ButterKnife.bind(this);
        sharedPreferences = ActivityUtil.getSharedPreferences(getContext());
        int sound = sharedPreferences.getInt(Constants.SOUND_KEY, 50);
        if (Constants.IS_FRIST_OPENDOOR) {
            MediaPlayerUtils.playSound(getContext(),R.raw.check_cabinet_open);
            Constants.IS_FRIST_OPENDOOR = false;
        }

        userInfoBean = (NewUserInfoBean.DataBean) ObjectSaveUtil.readObject(getContext(), Constants.USER_INFO_BEAN, Constants.USER_INFO_BEAN);
        Message message = allHandler.obtainMessage();
        message.obj = userInfoBean.getRoleInfoId();
        message.what = HandlerConstant.ROLL;
        allHandler.sendMessage(message);

    }

    @OnClick({R.id.iv_faster_opendoor_1, R.id.iv_faster_opendoor_2, R.id.iv_faster_opendoor_3,
            R.id.iv_faster_opendoor_4, R.id.iv_faster_opendoor_5, R.id.iv_faster_close})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_faster_opendoor_1:
                clickCabinet(tvFasterOpendoor1, isClickOne, 1);
                break;
            case R.id.iv_faster_opendoor_2:
                clickCabinet(tvFasterOpendoor2, isClickSecond, 2);
                break;
            case R.id.iv_faster_opendoor_3:
                clickCabinet(tvFasterOpendoor3, isClickThird, 3);
                break;
            case R.id.iv_faster_opendoor_4:
                clickCabinet(tvFasterOpendoor4, isClickFour, 4);
                break;
            case R.id.iv_faster_opendoor_5:
                clickCabinet(tvFasterOpendoor5, isClickFive, 5);
                break;
            case R.id.iv_faster_close:

                if (selectCabinet != 0) {
                    Message message1 = allHandler.obtainMessage();
                    message1.obj = selectCabinet;
                    message1.what = HandlerConstant.FLASH_OFF;
                    allHandler.sendMessage(message1);
                }

                Constants.IS_SHOW_FASTER_OPENDOOR = false;
                cancel();
                break;
        }
    }

    /**
     * 点击柜子  将柜子的编号改为 '开'
     *
     * @param tv        柜子的编号
     * @param isOnClick 是否可点击
     * @param cabinet   柜号
     */

    private void clickCabinet(TextView tv, boolean isOnClick, int cabinet) {
        CharSequence text = tv.getText();
        if (isOnClick) {
            if (text.equals(cabinet + "")) {
                tv.setText("开");
                tv.setTextColor(Color.WHITE);
                tv.setBackground(getContext().getResources().getDrawable(R.drawable.faster_dialog_select_bg_shape));
                selectCabinetClick(cabinet);
            } else {
                // 开门
                Message message = allHandler.obtainMessage();
                message.obj = selectCabinet;
                message.what = HandlerConstant.TAKE_OUT;
                allHandler.sendMessage(message);

            }
        }
    }

    /**
     * 初始化所有未点击的柜子UI
     *
     * @param i 柜号
     */
    private void selectCabinetClick(int i) {
        selectCabinet = i;
        if (beforeSelect == 1) {
            tvFasterOpendoor1.setText("1");
            tvFasterOpendoor1.setTextColor(getContext().getResources().getColor(R.color.date_bg_color));
            tvFasterOpendoor1.setBackground(getContext().getResources().getDrawable(R.drawable.faster_dialog_cabinet_shape));
        } else if (beforeSelect == 2) {
            tvFasterOpendoor2.setText("2");
            tvFasterOpendoor2.setTextColor(getContext().getResources().getColor(R.color.date_bg_color));
            tvFasterOpendoor2.setBackground(getContext().getResources().getDrawable(R.drawable.faster_dialog_cabinet_shape));
        } else if (beforeSelect == 3) {
            tvFasterOpendoor3.setText("3");
            tvFasterOpendoor3.setTextColor(getContext().getResources().getColor(R.color.date_bg_color));
            tvFasterOpendoor3.setBackground(getContext().getResources().getDrawable(R.drawable.faster_dialog_cabinet_shape));
        } else if (beforeSelect == 4) {
            tvFasterOpendoor4.setText("4");
            tvFasterOpendoor4.setTextColor(getContext().getResources().getColor(R.color.date_bg_color));
            tvFasterOpendoor4.setBackground(getContext().getResources().getDrawable(R.drawable.faster_dialog_cabinet_shape));
        } else if (beforeSelect == 5) {
            tvFasterOpendoor5.setText("5");
            tvFasterOpendoor5.setTextColor(getContext().getResources().getColor(R.color.date_bg_color));
            tvFasterOpendoor5.setBackground(getContext().getResources().getDrawable(R.drawable.faster_dialog_cabinet_shape));
        }

        // 当前选择的柜子 开启 闪灯
        Message message = allHandler.obtainMessage();
        message.obj = selectCabinet;
        message.what = HandlerConstant.FLASH_ON;
        allHandler.sendMessage(message);

        if (beforeSelect != 0) {
            // 上一个柜子 关灯
            Message message1 = allHandler.obtainMessage();
            message1.obj = beforeSelect;
            message1.what = HandlerConstant.FLASH_OFF;
            allHandler.sendMessageDelayed(message1, 50);
        }

        beforeSelect = i;
    }

    /**
     * 初始化柜子的连接状态
     *
     * @param address 单片机地址
     */

    private void initSharedPreferences(String address) {

        if (sharedPreferences != null) {
            if (sharedPreferences.getString(Constants.ADDRESS_FIRST, "").equals(address)) {
                tvFasterOpendoor1.setVisibility(View.VISIBLE);
                ivFasterOpendoor1.setImageResource(R.mipmap.skmguiziatl);
                isClickOne = true;
                Log.e("CCC", "ADDRESS_FIRST: " + sharedPreferences.getString(Constants.ADDRESS_SECOND, ""));
            } else if (sharedPreferences.getString(Constants.ADDRESS_SECOND, "").equals(address)) {
                tvFasterOpendoor2.setVisibility(View.VISIBLE);
                ivFasterOpendoor2.setImageResource(R.mipmap.skmguiziatl);
                isClickSecond = true;
                Log.e("CCC", "ADDRESS_SECOND: " + sharedPreferences.getString(Constants.ADDRESS_SECOND, ""));
            } else if (sharedPreferences.getString(Constants.ADDRESS_THIRD, "").equals(address)) {
                tvFasterOpendoor3.setVisibility(View.VISIBLE);
                ivFasterOpendoor3.setImageResource(R.mipmap.skmguiziatl);
                isClickThird = true;
                Log.e("CCC", "ADDRESS_THIRD: " + sharedPreferences.getString(Constants.ADDRESS_SECOND, ""));
            } else if (sharedPreferences.getString(Constants.ADDRESS_FOURTH, "").equals(address)) {
                tvFasterOpendoor4.setVisibility(View.VISIBLE);
                ivFasterOpendoor4.setImageResource(R.mipmap.skmguiziatl);
                isClickFour = true;
                Log.e("CCC", "ADDRESS_FOURTH: " + sharedPreferences.getString(Constants.ADDRESS_SECOND, ""));
            } else if (sharedPreferences.getString(Constants.ADDRESS_FIFTH, "").equals(address)) {
                tvFasterOpendoor5.setVisibility(View.VISIBLE);
                ivFasterOpendoor5.setImageResource(R.mipmap.skmguiziatl);
                isClickFive = true;
                Log.e("CCC", "ADDRESS_FIFTH: " + sharedPreferences.getString(Constants.ADDRESS_SECOND, ""));
            }
        }
    }

    @Override
    public void onHandleFrameMessage(Message msg) {
        super.onHandleFrameMessage(msg);
        switch (msg.what) {
            case HandlerConstant.ROLL_SUCCESS:
                initSharedPreferences((String) msg.obj);
                break;

            case HandlerConstant.NO_OPERATION:
                Message message = allHandler.obtainMessage();
                message.obj = selectCabinet;
                message.what = HandlerConstant.FLASH_OFF;
                allHandler.sendMessage(message);
                break;

            case HandlerConstant.OPEN_DOOR:
                if (isShowing()) {
                    Intent intent = new Intent(getContext(), OpenDoorRemindActivity.class);
                    intent.putExtra(Constants.OPEN_DOOR_REMIND, Constants.OPEN);
                    intent.putExtra(Constants.OPEN_DOOR_CABINET, selectCabinet);
                    getContext().startActivity(intent);
                    cancel();
                }
                break;
        }
    }

    @Override
    public void cancel() {
        super.cancel();
        MediaPlayerUtils.stop();
    }
}
